/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
Ext.chart.Chart.CHART_URL = '../../../ext3/resources/charts.swf';
Ext.QuickTips.init();
var _pageSize = 20;
var _title = '单位月数据锁定';
var store;

var loadServlet = '../../../servlet/data/YsjLoad';

var tabs = new Ext.TabPanel({
    activeTab: 0,
    plain: true,
    enableTabScroll: true,
    defaults: {autoScroll: true},
    items: [{
            title: currentOrgName + _title,
            id: 'main-tab',
            layout: "fit",
            tbar: new Ext.Toolbar([
                {
                    text: '月数据锁定',
                    iconCls: 'icon-lock',
                    handler: lock
                            , style: {marginLeft: '10px'}

                },
                {
                    text: '月数据解锁',
                    iconCls: 'icon-unlock',
                    handler: unlock
                }, '-', yearToCombo, lblYear, monthToCombo, lblMonth,
                new tlf.component.RangeCombo({
                    id: 'RangeThis',
                    listeners: {
                        select: function(combo, record, index) {
                            store.baseParams.range = combo.getValue();
                        }
                    }
                }),
                new tlf.component.ReportStatusCombo({
                    id: 'ReportStatusThis',
                    listeners: {
                        select: function(combo, record, index) {
                            store.baseParams.reportStatus = combo.getValue();
                        }
                    }
                }),
                searchButton
            ])
        }
    ]
});

var hourSeries = [
    {
        yField: 'voltage',
        displayName: '电压值',
        style: {
            fillColor: 0xAAAAFF,
            borderColor: 0x3333FF,
            lineColor: 0x3333FF
        }
    }
];
var extraStyle = {
    xAxis: {
        majorGridLines: {color: 0x999999, size: 1}
    },
    yAxis: {
        titleRotation: -90
    },
    legend: {
        display: "right",
        padding: 5,
        spacing: 2,
        font: {color: 0x000000, family: "Arial", size: 12},
        border: {size: 1, color: 0x999999}
    }

};
function lock() {
    if (currentOrgCode.length !== 4) {
        Ext.Msg.alert("系统提示", "只允许市级公司锁定数据");
        return;
    }
    if (store.getCount() <= 0) {
        Ext.Msg.alert("系统提示", "没有数据可以锁定");
        return;
    }
    var date = Ext.getCmp("YearToCombo").getValue();
    var month = Ext.getCmp('MonthToCombo').getValue();    
    var reportStatus = -1;
    var range = -1;
    var token1 = new Date().getTime();
    Ext.Ajax.request({
        url: '../../../servlet/data/LockServlet',
        success: function(response, options) {
            var obj = Ext.decode(response.responseText);
            if (obj.success) {
                store.reload();
                Ext.Msg.alert("系统提示", "锁定成功");
            } else {
                Ext.Msg.alert("系统提示", obj.msg);
            }
        },
        failure: function(response, opts) {
            Ext.Msg.alert('系统提示', '锁定失败，清稍候再试 ' + response.status);
        },
        params: {token:token,token1:token1,cmd: "lockYOrg", date: date + "" + month, reportStatus: reportStatus, range: range}
    });
}


function unlock() {
    if (currentOrgCode.length !== 4) {
        Ext.Msg.alert("系统提示", "只允许市级公司解锁数据");
        return;
    }
    Ext.MessageBox.confirm('系统提示', '解锁将清除所用锁定的数据，确实要解锁数据吗?', function(btn) {
        if (btn === 'yes') {
            var date = Ext.getCmp("YearToCombo").getValue();
            var month = Ext.getCmp('MonthToCombo').getValue();
            var token1 = new Date().getTime();
            Ext.Ajax.request({
                url: '../../../servlet/data/LockServlet',
                success: function(response, options) {
                    var obj = Ext.decode(response.responseText);
                    if (obj.success) {
                        store.reload();
                        Ext.Msg.alert("系统提示", "解锁成功");
                    } else {
                        Ext.Msg.alert("系统提示", obj.msg);
                    }
                },
                failure: function(response, opts) {
                    Ext.Msg.alert('系统提示', '解锁失败，清稍候再试 ' + response.status);
                },
                params: {token:token,token1:token1,cmd: "unlockYOrg", date: date + "" + month}
            });            
        }
    });
}


var checkboxSel = new Ext.grid.CheckboxSelectionModel();
function buildWindow() {
    store = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'id',
        totalProperty: 'count',
        fields: fieldName.concat(["lockStatus"]),
        autoLoad: true,
        baseParams: {cmd: 'ysjOrg', yearTo: thisYear, monthTo: now.format("m"), reportStatus: 1, start: 0, limit: _pageSize}
    });

    var grid = new Ext.grid.GridPanel({
        store: store,
        //sm: checkboxSel,
        columns: [{header: "状态", width: 50, dataIndex: "lockStatus",
                renderer:function (value, metaData, record, rowIndex, colIndex, store){
                    //alert(value);
                    //alert(typeof value);
                    var imageName;
                    if(value===true){
                        imageName = 'lock.png';
                        return "<image src='" + basePath + "image/icon/" + imageName + "'/>";
                    }else{
                        return "";
                    }
                }
            }].concat(columns),
        loadMask: true,
        bbar: new Ext.PagingToolbar({
            pageSize: _pageSize,
            store: store,
            displayInfo: true,
            displayMsg: '显示记录：{0} - {1}   总记录：{2}',
            emptyMsg: '目前没有信息'
        })
    });
    //Ext.getCmp('ReportStatus').setValue(1);
    Ext.getCmp('main-tab').add(grid);
    Ext.getCmp('YearToCombo').setValue(now.add(Date.MONTH,-1).format("Y"));
    Ext.getCmp('MonthToCombo').setValue(now.add(Date.MONTH,-1).format("m"));
    var applicationUI = new Ext.Viewport({
        layout: "fit",
        items: [
            tabs
        ]
    });
}
Ext.onReady(buildWindow);
